<?php 
namespace app\admin\controller;

use app\common\model\banner\Banner as BannerModel;
use app\common\controller\Base64;
use think\Config;
use think\Db;
use think\Exception;
use app\common\controller\Log;

class Banner extends Base
{
	/**
	 * 获取轮播图列表
	 * @return unknown
	 */
	public function bannerList()
	{
		$list = BannerModel::where(['state' => 1])->select();
		return jsonSuccess($list);
	}
	
	/**
	 * 上传图片
	 * @return unknown
	 */
	public function upBanner()
	{
		$img = $this->request->post('img');
		$base64 = new Base64();
		$res = $base64 ->up_base64($img, Config::get('path.banner'));
		return jsonSuccess($res);
	}
	
	/**
	 * 添加轮播图
	 * @return unknown
	 */
	public function addBanner()
	{
		$data = $this->request -> post('');
		Db::startTrans();
		try {
			//添加轮播图
			$data['create_time'] = time();
			$data['update_time'] = time();
			BannerModel::create($data);
			//添加日志
			Log::addLog(1, '添加轮播图');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('轮播图上传成功');
	}
	
	/**
	 * 删除轮播图
	 * @return unknown
	 */
	public function deleteBanner()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			$banner = BannerModel::get($id);
			$banner -> save(['update_time' => time(),'state' => 0]);
			//添加日志
			Log::addLog(3, '删除轮播图');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('删除成功');
	}
}